package com.jt9003.controller;


import com.jt9003.pojo.Usage;
import com.jt9003.service.UsageService;
import com.jt9003.utils.PageUtils;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/usage")
public class UsageController {
    @Resource
    private UsageService usageService;

    @RequestMapping("/insertUsage")
    public Map<String, Object> insertUsage(String usageName) {
        Map<String, Object> map = new HashMap();
        System.out.println("usageName="+usageName);
        int i = usageService.insertUsage(usageName);
        if (i > 0) {
            map.put("msg", true);
            map.put("data", usageName);
        } else {
            map.put("msg", "error");
        }
        return map;
    }

    @RequestMapping("/deleteUsageById")
    public Map<String, Object> deleteUsageById(int usageId) {
        Map<String, Object> map = new HashMap();
        int i = usageService.deleteUsageById(usageId);
        if (i > 0) {
            map.put("msg", true);
        } else {
            map.put("msg", "error");
        }
        return map;
    }

    @RequestMapping("/updateUsageById")
    public Map<String, Object> updateUsageById(Usage usage) {
        Map<String, Object> map = new HashMap();
        int i = usageService.updateUsageById(usage);
        System.out.println(usage.getUsageId() + "---" + usage.getUsageName());
        if (i > 0) {
            map.put("msg", true);
            map.put("data", usage);
        } else {
            map.put("msg", false);
        }
        System.out.println("map="+map);
        return map;
    }

    @RequestMapping("/updateUsageStateById")
    public Map<String, Object> updateUsageStateById(Usage usage) {
        Map<String, Object> map = new HashMap();
        int i = usageService.updateUsageStateById(usage);
        System.out.println(i);
        if (i > 0) {
            map.put("msg", true);
            map.put("code", "0");
            map.put("data", usage);
        } else {
            map.put("msg", "error");
        }
        return map;
    }

    @RequestMapping("/selectUsageByNameOrPinyinInitials")
    public Map<String, Object> selectUsageByNameOrPinyinInitials(String usageName, @RequestParam(value = "page", required = false, defaultValue = "1") Integer page,
                                                                 @RequestParam(value = "limit", required = false, defaultValue = "5") Integer limit) {
        Map<String, Object> map = new HashMap();
        int start = PageUtils.getStart(page, limit);
        int count = usageService.getUsageCountByResult(usageName);
        List<Usage> list = usageService.selectUsageByNameOrPinyinInitials(usageName, start, limit);
        if (list != null) {
            map.put("msg", "success");
            map.put("data", list);
            map.put("code", "0");
            map.put("count", count);
            map.put("", "");
        } else {
            map.put("msg", "error");
        }
        return map;
    }

    @RequestMapping("/getUsageCountByResult")
    public Map<String, Object> getUsageCountByResult(String usageName) {
        Map<String, Object> map = new HashMap();
        int i = usageService.getUsageCountByResult(usageName);
        if (i > 0) {
            map.put("status", "200");
            map.put("message", "success");
            map.put("data", i);
        } else {
            map.put("msg", "error");
        }
        return map;
    }

    @RequestMapping("/selectUsageList")
    public Map<String, Object> selectUsageList(@RequestParam(value = "page", required = false, defaultValue = "1") Integer page,
                                               @RequestParam(value = "limit", required = false, defaultValue = "5") Integer limit) {
        Map<String, Object> map = new HashMap();
        int start = PageUtils.getStart(page, limit);
        int count = usageService.getUsageCount();
        List<Usage> list = usageService.selectUsageList(start, limit);
        if (list.size() > 0) {
            System.out.println("================start:" + start);
            map.put("msg", true);
            map.put("data", list);
            map.put("code", "0");
            map.put("count", count);
            map.put("", "");
        } else {
            map.put("msg", "error");
        }
        return map;
    }

    @RequestMapping("/deleteUsagesById")
    public Map<String, Object> deleteUsagesById(String checkList) {
        Map<String, Object> map = new HashMap();
        System.out.println("==>" + checkList);

        String[] strs = checkList.split(",");
        System.out.println(strs[0]);
        List<Integer> ids = new ArrayList<>();
        for (String str : strs) {
            ids.add(Integer.parseInt(str));

        }

        int i = usageService.deleteUsagesById(ids);
        if (i > 0) {
            map.put("msg", true);
        } else {
            map.put("msg", "error");
        }
        return map;
    }

    @RequestMapping("/getUsageCount")
    public Map<String, Object> getUsageCount() {
        Map<String, Object> map = new HashMap();
        int i = usageService.getUsageCount();
        if (i > 0) {
            map.put("status", "200");
            map.put("message", "success");
            map.put("data", i);
        } else {
            map.put("msg", "error");
        }
        return map;
    }


}
